package com.dh.bm.service;

import com.dh.bm.dao.BookDao;
import com.dh.bm.dao.CategoryDao;
import com.dh.bm.dao.impl.BookDaoImpl;
import com.dh.bm.dao.impl.CategoryDaoImpl;
import com.dh.bm.entity.Book;
import com.dh.bm.entity.Category;
import com.dh.bm.entity.vo.BookVo;

import java.util.ArrayList;
import java.util.List;

public class BookService {
    private BookDao bookDao = new BookDaoImpl();
    private CategoryDao categoryDao = new CategoryDaoImpl();

    public List<Book> queryAll() {
        return bookDao.selectAll();
    }

    public BookVo queryById(int id) {
        Book book = bookDao.selectById(id);
        Integer cid = book.getCid();
        // 根据分类cid去查分类的名字
        Category category = categoryDao.selectById(cid);
        BookVo bookVo = new BookVo();
        bookVo.setBid(book.getBid());
        bookVo.setName(book.getName());
        bookVo.setAuthor(book.getAuthor());
        bookVo.setCname(category.getName());
        return bookVo;
    }

    // 插入书籍
    public boolean add(Book book) {
        return bookDao.insert(book);
    }

    public boolean update(int id, Book book) {
        return bookDao.updateById(id, book);
    }


}
